<?php
	jimport('joomla.application.helper');
	require_once( JApplicationHelper::getPath( 'html' ) );
	JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.$option.DS.'tables');
	switch($task)
	{
		case 'view':
			viewReview($option);
			break;
		case 'comment':
			addComment($option);
			break;
	  default:
	    showPublishedReviews($option);
	    break;
	}
	function showPublishedReviews($option)
	{
	  $db =& JFactory::getDBO();
	  $query = "SELECT * FROM #__reviews WHERE 
	          published = '1' ORDER BY review_date DESC";
	  $db->setQuery( $query );
	  $rows = $db->loadObjectList();
	  if ($db->getErrorNum()) 
	  {
	    echo $db->stderr();
	    return false;
	  }
	  HTML_reviews::showReviews($rows, $option);
	}
	function viewReview($option)
	{
	  $id = JRequest::getVar('id', 0);
	  $row =& JTable::getInstance('review', 'Table');
	  $row->load($id);
	  if(!$row->published)
	  {
	    JError::raiseError( 404, JText::_( 'Invalid ID provided' ) );
	  }
	  HTML_reviews::showReview($row, $option);
	  $db =& JFactory::getDBO();
	  $db->setQuery("Select * from #__reviews_comments where review_id = '$id'");
	  $rows = $db->loadObjectList();
	  foreach ($rows as $row)
	  {
	  	HTML_reviews::showComment($row);
	  }
	  $user =& JFactory::getUser();
	  if ($user->name) {
	  	$name = $user->name;
	  }else
	  {
	  	$name = '';
	  }
	  HTML_reviews::showCommentForm($option,$id,$name);
	}
	function addComment($option)
	{
		global $mainframe;
		  $row =& JTable::getInstance('comment', 'Table');
		  if (!$row->bind(JRequest::get('post'))) 
		  {
		    echo "<script> alert('".$row->getError()."'); 
		                   window.history.go(-1); </script>\n";
		    exit();
		  }
		  $row->comment_date = date( 'Y-m-d H:i:s' );
		  $user =& JFactory::getUser();
		  if($user->id)
		  {
		    $row->user_id = $user->id;
		  }
		  if (!$row->store()) 
		  {
		    echo "<script> alert('".$row->getError()."'); 
		                  window.history.go(-1); </script>\n";
		    exit();
		  }
		  $mainframe->redirect('index.php?option=' . 
		                  $option . '&id=' . $row->review_id . 
		                 '&task=view', 'Comment Added.');
	}
?>